package com.gdit.zax.utils;

import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.gdit.zax.Constant;

public class webUtil {
	public static void printNetData (HttpServletRequest request) {
		LogUtil.print("=====================header data========================");
		printHeader(request);
		LogUtil.print("=====================Parameter data========================");
		printParameter(request);
		LogUtil.print("=====================end========================");
	}
	
	public static void printHeader (HttpServletRequest request) {
		Enumeration e = request.getHeaderNames();
		
		while (e.hasMoreElements()) {
			Object obj = e.nextElement();
			LogUtil.print(obj + " = " + request.getHeader(obj.toString()));
		}
	}
	
	public static void printParameter (HttpServletRequest request) {
		Enumeration e = request.getParameterNames();
		
		while (e.hasMoreElements()) {
			Object obj = e.nextElement();
			LogUtil.print(obj + " = " + request.getParameter(obj.toString()));
		}
	}
	
	
	public static String getCookie(HttpServletRequest request,String key) {
		for (Cookie cook:request.getCookies()) {
			if (cook.getName().equals(key)) {
				return cook.getValue();
			}
		}
		return null;
	}
	
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getRemoteAddr();
		}
		return ip;
		}
	
	/**
	 * 获取web绝对路径
	 * @param str
	 * @return
	 */
	public static String getWebAbsoluteUrl(String path,String defpath) {
		if (TextUtil.isEmpty(path)) {
			path = defpath;
		}
		return Constant.WEBRoot + path;
	}
}
